package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.pubClass.SimpleSpinnerOption;

/**
 * Created by he on 2017/4/14.
 */

public class ListitemAdapter extends BaseAdapter implements Filterable {

    private MyFilter myFilter;
    private List<SimpleSpinnerOption> mList;
    private ArrayList<SimpleSpinnerOption> mOriginalValues;
    private Context mContext;
    private LayoutInflater mInflater;

    private final Object mLock = new Object();

    @Override
    public Filter getFilter() {
        if (myFilter == null) {
            myFilter = new MyFilter();
        }
        return myFilter;
    }

    public ListitemAdapter(Context context,List<SimpleSpinnerOption> list){
        mList = list;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList==null?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();

            convertView = mInflater.inflate(R.layout.check_list_item, null);
            holder.mTitle = (TextView)convertView.findViewById(R.id.title_li);
            holder.mSubTitile = (TextView)convertView.findViewById(R.id.subtitle_li);
            holder.mCheckBox = (CheckBox)convertView.findViewById(R.id.checkbox_li);
            convertView.setTag(holder);
        }else {

            holder = (ViewHolder)convertView.getTag();
        }

        holder.mTitle.setText((String)mList.get(position).getName());
        holder.mSubTitile.setText((String)mList.get(position).getValue());
        holder.mCheckBox.setChecked(mList.get(position).isChecked());
        return convertView;
    }

    public class ViewHolder{
        public TextView mTitle;
        public TextView mSubTitile;
        public CheckBox mCheckBox;
    };
    class MyFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            // 持有过滤操作完成之后的数据。该数据包括过滤操作之后的数据的值以及数量。 count:数量 values包含过滤操作之后的数据的值
            FilterResults results = new FilterResults();

            if (mOriginalValues == null) {
                synchronized (mLock) {
                    // 将list的用户 集合转换给这个原始数据的ArrayList
                    mOriginalValues = new ArrayList<SimpleSpinnerOption>(mList);
                }
            }
            if (prefix == null || prefix.length() == 0) {
                synchronized (mLock) {
                    ArrayList<SimpleSpinnerOption> list = new ArrayList<SimpleSpinnerOption>(
                            mOriginalValues);
                    results.values = list;
                    results.count = list.size();
                }
            } else {
                // 做正式的筛选
                String prefixString = prefix.toString().toLowerCase();

                // 声明一个临时的集合对象 将原始数据赋给这个临时变量
                final ArrayList<SimpleSpinnerOption> values = mOriginalValues;

                final int count = values.size();

                // 新的集合对象
                final ArrayList<SimpleSpinnerOption> newValues = new ArrayList<SimpleSpinnerOption>(
                        count);

                for (int i = 0; i < count; i++) {
                    // 如果姓名的前缀相符或者电话相符就添加到新的集合
                    final SimpleSpinnerOption value = (SimpleSpinnerOption) values.get(i);


                    if (value.getName().contains(prefixString)) {

                        newValues.add(value);
                    }
                }
                // 然后将这个新的集合数据赋给FilterResults对象
                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            // 重新将与适配器相关联的List重赋值一下
            mList = (List<SimpleSpinnerOption>) results.values;

            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }

}
